package com.niiwoo.civet.account.service.local.withdraw;/**
 * Created by DELL on 2017/12/5.
 */

import com.niiwoo.civet.account.dao.entity.WithdrawCancelOrder;
import com.niiwoo.civet.account.dao.entity.WithdrawCancelOrderExample;
import com.niiwoo.civet.account.dao.mapper.WithdrawCancelOrderMapper;
import com.niiwoo.civet.account.enums.WithdrawCancelOrderStatusEnum;
import com.niiwoo.tripod.lanmao.component.LanMaoSequence;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 提现取消订单表Service
 *
 * @author xiaoyingping
 * @create 2017-12-05 17:31
 **/
@Service
public class WithdrawCancelOrderService {

    @Autowired
    private WithdrawCancelOrderMapper withdrawCancelOrderMapper;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private LanMaoSequence lanMaoSequence;

    /**
     * 查询提现取消订单
     * @param requestNo
     * @return
     */
    public WithdrawCancelOrder getByRequestNo(String requestNo) {
        WithdrawCancelOrderExample example = new WithdrawCancelOrderExample();
        WithdrawCancelOrderExample.Criteria criteria = example.createCriteria();
        criteria.andRequestNoEqualTo(requestNo);

        List<WithdrawCancelOrder> withdrawCancelOrderList = withdrawCancelOrderMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(withdrawCancelOrderList)) {
            return null;
        }

        return withdrawCancelOrderList.get(0);
    }

    /**
     * 新增提现取消订单记录
     *
     * @param withdrawCancelOrder
     */
    public void addWithdrawCancelOrder(WithdrawCancelOrder withdrawCancelOrder) {
        withdrawCancelOrder.setId(snowflakeIdWorker.nextId());
        withdrawCancelOrder.setRequestNo(lanMaoSequence.getRequestNo());
        Date date = new Date();
        withdrawCancelOrder.setCreateTime(date);
        withdrawCancelOrder.setUpdateTime(date);
        withdrawCancelOrderMapper.insertSelective(withdrawCancelOrder);
    }

    /**
     * 更新提现取消订单的状态
     * @param withdrawCancelOrderId
     * @param statusEnum
     * @param updateUserId
     * @return
     */
    public int updateStatus(Long withdrawCancelOrderId, WithdrawCancelOrderStatusEnum statusEnum, Long updateUserId){
        Assert.notNull(withdrawCancelOrderId, "withdrawCancelOrderId不能为空");
        Assert.notNull(statusEnum, "statusEnum不能为空");
        Assert.notNull(updateUserId, "updateUserId不能为空");

        //修改状态
        WithdrawCancelOrder record = new WithdrawCancelOrder();
        record.setId(withdrawCancelOrderId);
        record.setStatus(statusEnum.getCode());
        record.setUpdateManager(updateUserId);
        record.setUpdateTime(new Date());
        return withdrawCancelOrderMapper.updateByPrimaryKeySelective(record);

    }



}
